# encoding: utf-8
#语音命令识别
import smbus
import time
import numpy
import myclient
#import keyboard
class ASR:

    # Global Variables
    address = None
    bus = None
    
    ASR_RESULT_ADDR = 100
    #识别结果存放处，通过不断读取此地址的值判断是否识别到语音，不同的值对应不同的语音

    ASR_WORDS_ERASE_ADDR = 101
    #擦除所有词条

    ASR_MODE_ADDR = 102
    #识别模式设置，值范围1~3
    #1：循环识别模式。状态灯常亮（默认模式）    
    #2：口令模式，以第一个词条为口令。状态灯常灭，当识别到口令词时常亮，等待识别到新的语音,并且读取识别结果后即灭掉
    #3：按键模式，按下开始识别，不按不识别。支持掉电保存。状态灯随按键按下而亮起，不按不亮

    ASR_ADD_WORDS_ADDR = 160
    #词条添加的地址，支持掉电保存

    def __init__(self, address, bus=1):
        self.address = address
        self.bus = smbus.SMBus(bus)
        
    def readByte(self):
        return self.bus.read_byte(self.address)

    def writeByte(self, val):               
        value = self.bus.write_byte(self.address, val)
        if value != 0:
            return False
        return True
    
    def writeData(self, reg, val):
        self.bus.write_byte(self.address,  reg)
        self.bus.write_byte(self.address,  val)

    def getResult(self):
        if ASR.writeByte(self, self.ASR_RESULT_ADDR):
            return -1        
        value = self.bus.read_byte(self.address)
        return value
    '''
    * 添加词条函数，
    * idNum：词条对应的识别号，1~255随意设置。识别到该号码对应的词条语音时，
    *        会将识别号存放到ASR_RESULT_ADDR处，等待主机读取，读取后清0
    * words：要识别汉字词条的拼音，汉字之间用空格隔开
    * 
    * 执行该函数，词条是自动往后排队添加的。   
    '''
    def addWords(self, idNum, words):
        buf = [idNum]       
        for i in range(0, len(words)):
            buf.append(eval(hex(ord(words[i]))))
        self.bus.write_i2c_block_data(self.address, self.ASR_ADD_WORDS_ADDR, buf)
        time.sleep(0.05)
        
    def eraseWords(self):
        result = self.bus.write_byte_data(self.address, self.ASR_WORDS_ERASE_ADDR, 0)
        time.sleep(0.06)
        if result != 0:
           return False
        return True
    
    def setMode(self, mode): 
        result = self.bus.write_byte_data(self.address, self.ASR_MODE_ADDR, mode)
        if result != 0:
           return False
        return True
        

if __name__ == "__main__":
    addr = 0x79 #传感器iic地址
    asr = ASR(addr)
    myRaspConnection = myclient.connect_Raspberry('49.233.245.232', 8888)
    myRaspConnection.send("2_0_-1_-1#")
    #添加的词条和识别模式是可以掉电保存的，第一次设置完成后，可以将1改为0
    if 1:
        asr.eraseWords()
        asr.setMode(1)
        asr.addWords(1, 'zuo zhuan')
        asr.addWords(1, 'wang zuo bian')
        asr.addWords(1, 'xiang zuo zhuan')
        asr.addWords(2, 'you zhuan')
        asr.addWords(2, 'xiang you zhuan')
        asr.addWords(2, 'you bian')
        asr.addWords(18, 'guo lai')
        asr.addWords(18,'qian jin')
        asr.addWords(18, 'kao jin dian')
        asr.addWords(3, 'wang qian zou')
        asr.addWords(3, 'yi zhi zou')
        asr.addWords(4, 'zou kai')
        asr.addWords(4, 'li kai ')
        asr.addWords(4, 'hou tui')
        asr.addWords(5, 'pa xia')
        asr.addWords(5, 'pa')
        asr.addWords(6, 'zuo xia')
        asr.addWords(6, 'zuo yi xia')
        asr.addWords(7, 'tiao wu')
        asr.addWords(7, 'niu yi niu')
        asr.addWords(7, 'wu dao')
        asr.addWords(8, 'zuo ge dong zuo')
        asr.addWords(8,'fu wo cheng')
        asr.addWords(9, 'da ge zhao hu')
        asr.addWords(9, 'ni hao')
        asr.addWords(9, 'ni hao ya')
        asr.addWords(9, 'ha lou')
        asr.addWords(10,'tiao')
        asr.addWords(10,'tiao yue')
        asr.addWords(11,'peng quan')
        asr.addWords(12,'wo shou')
        asr.addWords(12,'shou')
        asr.addWords(12,'tai shou')
        asr.addWords(13,'hao')
        asr.addWords(13,'dui le')
        asr.addWords(13,'fei chang hao')
        asr.addWords(13,'dui')
        asr.addWords(14,'cuo le')
        asr.addWords(14,'bu dui')
        asr.addWords(14,'cuo')
        asr.addWords(15,'tai kong bu')
        asr.addWords(16,'ting xia')
        asr.addWords(16,'ting zhi')
        asr.addWords(16,'ting')
        asr.addWords(17,'zhuan quan')
        asr.addWords(17,'zhuan ge quan')
        asr.addWords(17,'xuan zhuan')
        asr.addWords(19, 'pao')
        asr.addWords(19, 'pao bu')
        asr.addWords(19, 'pao qi lai')
        asr.addWords(20,'hua bu')
        asr.addWords(20,'hua guo lai')
        asr.addWords(21,'xiang hou zhuan')
        asr.addWords(21,'xiang hou kan')
        asr.addWords(21,'hui tou')
        asr.addWords(250,'guan bi')
        
    index=0
    while 1:
        data = asr.getResult()
        if data!=0:
            msg = "2_"+str(int(data))+"_-1_-1#"
            myRaspConnection.send(msg)
        if data==250:
            msg = "0_0_-1_-1#"
            myRaspConnection.send(msg)
            break
        time.sleep(0.5)
        index+=1
        if index==100:
            index=0
            msg = "2_0_-1_-1#"
            myRaspConnection.send(msg)
